package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.framework.BasePresenter;
import com.taomu.app.zongchouhuli.moudle.mine.contract.ModifyNickNameContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.ModifyNickNamePresenter;
import com.taomu.app.zongchouhuli.utils.BaseConst;

public class ModifyNickNameAcitivty extends BaseActivity<ModifyNickNameContract.Presenter> implements ModifyNickNameContract.View {

    private ImageView mBackIm;

    private EditText mModifyEt;

    private TextView mSaveTv;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(mPresenter != null){
            mPresenter.onDetch();
        }
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_modify_nick;
    }

    @Override
    protected void initView() {
        mBackIm = findViewById(R.id.modify_nick_back_im);
        mModifyEt = findViewById(R.id.modify_nick_edit);
        mSaveTv = findViewById(R.id.modify_nick_save_tv);
    }

    @Override
    protected void initData() {
        if(getIntent() != null){
            String nick = getIntent().getStringExtra(BaseConst.MODIFYNICK);
            mModifyEt.setText(nick);
        }
    }

    @Override
    protected void initEvent() {
        mSaveTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!TextUtils.isEmpty(getModifyNick())){
                    mPresenter.setNickName(getModifyNick());
                }else{
                    showToast("请输入昵称");
                }
            }
        });
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                back();
            }
        });
    }

    @Override
    protected ModifyNickNameContract.Presenter createPresenter() {
        return new ModifyNickNamePresenter(this);
    }

    private String getModifyNick(){
        return mModifyEt.getText().toString();
    }

    @Override
    public void setNickResults(String msg) {
        showToast(msg);
    }

    @Override
    public void setPresenter(ModifyNickNameContract.Presenter presenter) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            back();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void back(){
        Intent intent = new Intent();
        intent.putExtra(BaseConst.MODIFYNICK,getModifyNick());
        setResult(0x21,intent);
        finish();
    }
}
